package com.chinawlx.wlxteacher.ui.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.ui.fragment.WLXClazzListFragment;
import com.chinawlx.wlxteacher.ui.fragment.WLXTimeTableFragment;
import com.chinawlx.wlxteacher.utils.WLXActivityManagerUtil;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.widget.silidingmenu.DragLayout;
import com.chinawlx.wlxteacher.wlx_user;
import com.chinawlx.wlxteacher.wlx_userDao;
import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 课程表or班级界面
 *
 * Created by wlx on 16/8/9.
 */
public class WLXCourseOrClazzActivity extends WLXBaseActivity implements RadioGroup
        .OnCheckedChangeListener, View.OnClickListener {
    @BindView(R.id.rg_content)
    RadioGroup radioGroup;
    @BindView(R.id.rb_course)
    RadioButton course;
    @BindView(R.id.rb_clazz)
    RadioButton clazz;
    @BindView(R.id.dl)
    DragLayout dl;
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.ll_lesson_statistics_draglayout)
    LinearLayout lessonStatistics;
    @BindView(R.id.ll_opinion_draglayout)
    LinearLayout opinion;
    @BindView(R.id.ll_setting_draglayout)
    LinearLayout setting;
    @BindView(R.id.iv_icon_draglayout)
    ImageView iconBtn;
    @BindView(R.id.tv_nickname_draglayout)
    TextView nickName;
    @BindView(R.id.tv_telephone_draglayout)
    TextView telephone;
    @BindView(R.id.iv_search_enter)
    ImageView searchBtn;

    private List<Integer> selectList = Arrays.asList(R.id.rb_course, R.id.rb_clazz);
    private FragmentManager mFragmentManager;
    private ArrayList<Fragment> mFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WLXActivityManagerUtil.addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_course_or_clazz);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initView();
    }

    /**
     * 初始化用户的信息,在onResume中初始化,方便在更新用户头像之后这些信息及时更改
     */
    private void initUserMsg() {
        List<wlx_user> list = WLXGreenDaoUtil.getUserDao().queryBuilder().where
                (wlx_userDao.Properties.User_id.eq(WLXConstant.USER_ID)).list();
        if (list.size() > 0) {
            wlx_user user = list.get(0);
            String avatar_url = user.getAvatar_url();
            Glide.with(WLXCourseOrClazzActivity.this).
                    load(avatar_url).dontAnimate().centerCrop().placeholder(R.drawable
                    .place_holder).into(iconBtn);
            Glide.with(WLXCourseOrClazzActivity.this).
                    load(avatar_url).dontAnimate().centerCrop().placeholder(R.drawable
                    .place_holder).into(ivIcon);
            nickName.setText(user.getNickname());
            telephone.setText(user.getMobile());
        }
    }

    private void initView() {
        initFragmetns();
        initDragLayout();
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup.check(R.id.rb_course);

        ivIcon.setOnClickListener(this);
        lessonStatistics.setOnClickListener(this);
        opinion.setOnClickListener(this);
        iconBtn.setOnClickListener(this);
        searchBtn.setOnClickListener(this);
        setting.setOnClickListener(this);
    }

    private void initDragLayout() {
        dl.setDragListener(new DragLayout.DragListener() {
            //界面打开的时候
            @Override
            public void onOpen() {
            }

            //界面关闭的时候
            @Override
            public void onClose() {
            }

            //界面滑动的时候
            @Override
            public void onDrag(float percent) {
                ViewHelper.setAlpha(ivIcon, 1 - percent);
            }
        });
    }

    private void initFragmetns() {
        mFragment = new ArrayList<>();
        WLXTimeTableFragment wlxTimeTableFragment = new WLXTimeTableFragment();
        WLXClazzListFragment wlxClazzListFragment = new WLXClazzListFragment();
        mFragment.add(wlxTimeTableFragment);
        mFragment.add(wlxClazzListFragment);
        setDefaultFragment(mFragment.get(0));

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int position = selectList.indexOf(checkedId);
        switchFragment(mFragment.get(position));

    }

//    private void switchFragment(int position) {
//        FragmentTransaction transaction = mFragmentManager.beginTransaction();
//        mFragmentManager.popBackStack();
//        transaction.replace(R.id.fl_container_to_course, mFragment.get(position));
//        transaction.commit();
//    }

    private Fragment mContent;
    /**
     * 设置默认的fragment
     */
    private void setDefaultFragment(Fragment fm) {
        mFragmentManager = getSupportFragmentManager();
        FragmentTransaction mFragmentTrans = mFragmentManager.beginTransaction();

        mFragmentTrans.add(R.id.fl_container_to_course, fm).commit();

        mContent = fm;
    }

    public void switchFragment(Fragment to) {
        if (mContent != to) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(mContent).add(R.id.fl_container_to_course, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(mContent).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
            mContent = to;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_icon:
                dl.open();
                break;
            case R.id.ll_lesson_statistics_draglayout: //课次统计
                startActivity(WLXLessonStatisticsActivity.class);
                break;
            case R.id.ll_opinion_draglayout: //意见反馈
                startActivity(WLXOpinionActivity.class);
                break;
            case R.id.ll_setting_draglayout: //设置
                startActivity(WLXSettingActivity.class);
                break;
            case R.id.iv_icon_draglayout:  //头像
                startActivity(WLXPersonInfoActivity.class);
                break;
            case R.id.iv_search_enter: //搜索
                startActivity(WLXSearchActivity.class);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initUserMsg();
    }
}
